package com.htg.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;



public class DialogInput {

    private Context cxt;

    private AlertDialog alertDialog = null;


    public DialogInput(Context cxt) {
        this.cxt = cxt;
    }

    public void show(String content, String leftButtonText, String rightButtonText, OnDialogInputClickListener leftListener, OnDialogInputClickListener rightListener) {
        if (alertDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(cxt);
            alertDialog = builder.create();
        } else {
            if (alertDialog.isShowing()) {
                return;
            }
        }


        View view = LayoutInflater.from(cxt).inflate(R.layout.dialog_input, null);

        TextView tvContent = view.findViewById(R.id.tv_dialog_input_content);
        EditText editText = view.findViewById(R.id.et_dialog_input_input);
        TextView tvLeftButton = view.findViewById(R.id.tv_dialog_input_leftbutton);
        TextView tvRightButton = view.findViewById(R.id.tv_dialog_input_rightbutton);

        tvContent.setText(content);
        tvLeftButton.setText(leftButtonText);
        tvRightButton.setText(rightButtonText);

        tvLeftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismis();
                leftListener.onClick(editText.getText().toString());
            }
        });
        tvRightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismis();
                rightListener.onClick(editText.getText().toString());
            }
        });

        alertDialog.setView(view);
        alertDialog.show();

    }


    public void dismis() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
        alertDialog = null;
    }

    public interface OnDialogInputClickListener {
        void onClick(String input);
    }


}
